/*SXDj+UzVnkytqCOCr56eS8o+2NgXSRQgvzWlwspk9MxVkbnb0rQD/gYqT9vc1Z7L*/
/*
 * @Author: xiahui
 * @PageInfo: 表头编辑前
 * @Date: 2019-01-14 16:49:54
 * @Last Modified by: zhr
 * @Last Modified time: 2021-09-08 22:36:55
 */
import { PAGECODE, AREA, FIELDS, URL, UISTATUS } from '../../constance';
import { crossRuleUtils } from '../../../../../scmpub/scmpub/pub/tool';
import transtypeUtils from '../../../../../scmpub/scmpub/pub/tool/transtypeUtils';
import remoteRequest from '../../../pub/beforeEvents/remoteRequest';
import vbillcodeBeforeEvent from './vbillcodeBeforeEvent';
import {
	canRateDateModify,
	canRateModify,
	rateTypeSellFilter,
	isRowSelfMake,
	isBillSelfMake
} from '../../../../../scmpub/scmpub/pub/tool/currencyRateUtil';
const { beforeEdit } = transtypeUtils;

export default async function(props, moduleId, key, value, record) {
	// 编辑性控制
	let editFlag = true;

	if (FIELDS.ctrantypeid == key) {
		// 审批中心，交易类型字段统一不可编辑
		if (this.approveFlag) {
			return false;
		}
		// 发布交易类型后控制不可编辑
		editFlag = beforeEdit.call(this, key, FIELDS.ctrantypeid);
		if (!editFlag) {
			return editFlag;
		} else {
			let isModify = props.getUrlParam('status');
			let isflag = UISTATUS.modify == isModify ? false : true;
			return isflag;
		}
	} else if (FIELDS.nexchangerate == key) {
		// 折本汇率
		let corigcurrencyid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.corigcurrencyid).value; // 原币币种
		let ccurrencyid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.ccurrencyid).value; // 本位币
		let fratecategory = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.fratecategory).value; // 获取汇率类型

		if (!corigcurrencyid || corigcurrencyid == ccurrencyid) {
			return false;
		} else {
			return canRateModify.call(
				this,
				fratecategory,
				isBillSelfMake.call(this, fratecategory, AREA.cardTableId, 'vsrctype', [ '28' ])
			);
		}
	} else if (FIELDS.ngroupexchgrate == key || FIELDS.nglobalexchgrate == key) {
		// 集团本位币汇率 | 全局本位币汇率
		let corigcurrencyid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.corigcurrencyid).value; // 原币币种
		let ccurrencyid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.ccurrencyid).value; // 本位币

		let constance = {
			key: key,
			areaid: moduleId,
			params: {
				ccurrencyid: ccurrencyid,
				corigcurrencyid: corigcurrencyid
			}
		};

		editFlag = await remoteRequest(URL.headBeforeEdit, constance);
		if (!editFlag) {
			return false;
		}
	} else if (FIELDS.bsc == key) {
		// 委外
		let rowNum = props.cardTable.getNumberOfRows(AREA.cardTableId);
		if (rowNum < 1) return false;
		for (let num = 0; num < rowNum; num++) {
			let vsrccode = props.cardTable.getValByKeyAndIndex(AREA.cardTableId, num, FIELDS.vsrccode).value;
			if (vsrccode) return false;
		}
	} else if (FIELDS.noriprepaylimitmny == key) {
		// 预付款限额
		let rowNum = props.cardTable.getNumberOfRows(AREA.cardTableId);
		if (rowNum < 1) return false;
		let pk_financeorg = null;
		for (let num = 0; num < rowNum; num++) {
			pk_financeorg = props.cardTable.getValByKeyAndIndex(AREA.cardTableId, num, FIELDS.pk_financeorg).value;
			if (pk_financeorg) break;
		}

		if (pk_financeorg) {
			// 表体财务组织原始版本有值
			let constance = {
				key: key,
				areaid: moduleId,
				params: {
					pk_financeorg: pk_financeorg
				}
			};

			editFlag = await remoteRequest(URL.headBeforeEdit, constance);
			if (!editFlag) {
				return false;
			}
		} else {
			return false;
		}
	} else if (FIELDS.pk_payterm == key) {
		// 付款协议，变更时不能编辑付款协议
		let isModify = props.getUrlParam('status');
		let isflag = UISTATUS.modify == isModify ? false : true;
		return isflag;
	} else if (FIELDS.cvendorvid == key || FIELDS.cvendorid == key) {
		// 供应商，变更时不能编辑付款协议
		let isModify = props.getUrlParam('status');
		editFlag = UISTATUS.modify == isModify ? false : true;
	} else if (FIELDS.vbillcode == key) {
		// 单据号
		let info = {
			key: FIELDS.vbillcode,
			formareaid: AREA.cardFormId,
			pk_org_key: FIELDS.pk_org,
			billtype: 'Z2'
		};
		editFlag = await vbillcodeBeforeEvent.call(this, this.props, info);
	} else if (key == FIELDS.cratetype) {
		//组织汇率类型
		//补充参照过滤----------------------------
		let corigcurrencyid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.corigcurrencyid).value; // 原币币种
		let ccurrencyid = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.ccurrencyid).value; // 本位币
		if (!corigcurrencyid || corigcurrencyid == ccurrencyid) {
			return false;
		}
	} else if (key == FIELDS.dratedate) {
		//组织汇率来源日期
		let fratecategory = props.form.getFormItemsValue(AREA.cardFormId, FIELDS.fratecategory).value; // 获取汇率类型

		return canRateDateModify.call(
			this,
			fratecategory,
			isBillSelfMake.call(this, fratecategory, AREA.cardTableId, 'vsrctype', [ '28' ])
		);
	}

	// 执行交叉校验规则
	if (editFlag) {
		let record = null;
		let crossRuleParams = {
			props,
			key, //当前字段
			appcode: null, //小应用编码，如果是本应用，可为空
			pagecode: PAGECODE.cardPagecode, //页面编码
			headarea: AREA.cardFormId, //表头区域编码
			bodyarea: AREA.cardTableId, //表体区域编码
			isHead: true, //是否为表头区字段
			record, //当前表体行数据，如果是表头触发，可以为空
			pk_org_field: FIELDS.pk_org, //组织字段，注意为oid
			billtype: 'Z2', //单据类型
			transtypeid_field: FIELDS.ctrantypeid //交易类型id字段
		};
		crossRuleUtils.beforeEdit.call(this, crossRuleParams);
	}

	return editFlag;
}

/*SXDj+UzVnkytqCOCr56eS8o+2NgXSRQgvzWlwspk9MxVkbnb0rQD/gYqT9vc1Z7L*/